/* Create figures to show variations   */
clear all
set more off
cd ""    //target directory


clear
foreach i of num 2006/2009 {
use "./rawdata/aha_extract`i'.dta",clear
keep id mloczip
gen zipcode=substr(mloczip,1,5)
gen year=`i'
drop mloczip
tempfile aha`i'
save `aha`i''
}


clear
use `aha2006',clear
append using `aha2007',force
append using `aha2008',force
append using `aha2009',force
tempfile aha
save `aha'


clear
use "./prepare/allbasic.dta",clear
merge 1:1 id year using `aha'
keep if _merge==3
drop _merge
gen adopt=(prechs>1)

gen str prechs_vnd=""
replace prechs_vnd="Null" if prechs==1
replace prechs_vnd="Self-developed" if prechs==2
replace prechs_vnd="Cerner" if prechs==3
replace prechs_vnd="CPSI" if prechs==4
replace prechs_vnd="Healthland" if prechs==5
replace prechs_vnd="Eclipsys" if prechs==6
replace prechs_vnd="Epic" if prechs==7
replace prechs_vnd="GE" if prechs==8
replace prechs_vnd="HMS" if prechs==9
replace prechs_vnd="McKessons" if prechs==10
replace prechs_vnd="Siemens" if prechs==11
replace prechs_vnd="Meditech" if prechs==12
replace prechs_vnd="Others" if prechs==13


gen adpcons=1
gen cons=1
replace adpcons=0 if prechs==1
bys hsa year:egen totadp=total(adpcons)
bys hsa year:egen totabsadp=total(cons)
bys hsa year prechs:egen totvndadp=total(adpcons)
gen mktsh=totvndadp/totadp
replace mktsh=0 if totadp==0
bys hsa year:egen maxmsh=max(mktsh)
gen absmktsh=totvndadp/totabsadp
replace absmktsh=0 if absmktsh==.
bys hsa year:egen maxabsmsh=max(absmktsh)


****** unconditional market share
preserve
drop if maxabsmsh==0
drop if absmktsh<maxabsmsh
duplicates drop hsa year prechs,force


graph drop _all
foreach i of num 3/12 {
cdfplot maxabsmsh if prechs==`i',by(year) name(vnd`i') opt1(lc(green blue cyan red) lp(shortdash dash_dot longdash solid))
}
restore


****** conditional market share ### Fig A3 ###
preserve
drop if maxmsh==0
drop if mktsh<maxmsh
duplicates drop hsa year prechs,force

local vndlist "Cerner CPSI Healthland Eclipsys Epic GE HMS McKessons Siemens Meditech"

graph drop _all
foreach i in `vndlist' {
cdfplot maxmsh if prechs_vnd=="`i'",by(year) name(vnd`i') opt1(lc(green blue cyan red) lp(shortdash dash_dot longdash solid) xtitle("Local market share") aspectratio(0.6) legend(rows(1) label(1 "2006") label(2 "2007") label(3 "2008") label(4 "2009") size(vsmall) symxsize(*0.3) margin(zero)) title("`i'") scheme(s1color) yline(0.2 0.4 0.6 0.8 1,lc(gs8) lp(dot)) xline(0.2 0.4 0.6 0.8 1,lc(gs8) lp(dot)))  
graph export "./results/condcdf_prechs_`i'.png",replace
}

 
grc1leg vndCerner vndCPSI vndHealthland vndEclipsys vndEpic vndGE vndHMS vndMcKessons vndSiemens vndMeditech, rows(3) altshrink legendfrom(vndCerner) scheme(s1color) //create Fig---cdf for the market share of leading vendors
restore





******************* CDF for adoption rate across markets for each year ### Fig A1 ###
*******************
*******************
clear
foreach i of num 2006/2009 {
use "./rawdata/aha_extract`i'.dta",clear
keep id mloczip
gen zipcode=substr(mloczip,1,5)
gen year=`i'
drop mloczip
tempfile aha`i'
save `aha`i''
}


clear
use `aha2006',clear
append using `aha2007',force
append using `aha2008',force
append using `aha2009',force
tempfile aha
save `aha'


clear
use "./prepare/allbasic.dta",clear
merge 1:1 id year using `aha'
keep if _merge==3
drop _merge
gen adopt=(prechs>1)


gen const=1
bys hsa year mhsmemb:egen tot=total(const)
bys hsa year mhsmemb:egen totadp=total(adopt)
gen pradp=totadp/tot
replace pradp=0 if pradp==.
duplicates drop hsa year mhsmemb,force

graph drop _all

foreach i of num 2006/2009 {
cdfplot pradp if year==`i',by(mhsmemb) name(yr`i') opt1(lc(green red) lp(solid dash) xtitle("Adoption rate") aspectratio(0.6) legend(rows(1) label(1 "Stand-alone") label(2 "Affiliated") size(vsmall) symxsize(*0.3) margin(zero)) title("Year `i'") scheme(s1color) yline(0.2 0.4 0.6 0.8 1,lc(gs8) lp(dot)) xline(0.2 0.4 0.6 0.8 1,lc(gs8) lp(dot))) 
}



grc1leg yr2006 yr2007 yr2008 yr2009, rows(2) altshrink legendfrom(yr2006) scheme(s1color) //create Fig---cdf for adoption rates across markets







******************* \# of unique vendors per market ### Fig A2 ###
*******************
*******************
clear all
set more off


clear
foreach i of num 2006/2009 {
use "./rawdata/aha_extract`i'.dta",clear
keep id mloczip
gen zipcode=substr(mloczip,1,5)
gen year=`i'
drop mloczip
tempfile aha`i'
save `aha`i''
}


clear
use `aha2006',clear
append using `aha2007',force
append using `aha2008',force
append using `aha2009',force
tempfile aha
save `aha'


clear
use "./prepare/allbasic.dta",clear
merge 1:1 id year using `aha'
keep if _merge==3
drop _merge

bys hsa year prechs:gen nvals = _n == 1
replace nvals=0 if prechs==1|prechs==13
bys hsa year:replace nvals = sum(nvals)
bys hsa year:replace nvals = nvals[_N]

duplicates drop hsa year,force


*** Four figures
graph drop _all

foreach i of num 2006/2009 {
cdfplot nvals if year==`i',name(yr`i') opt1(xtitle("# leading vendors") aspectratio(0.6) legend(off) title("Year `i'") scheme(s1color) yline(0.2 0.4 0.6 0.8 1,lc(gs8) lp(dot)) xline(1 2 3 4 5 6 7 8 9 10,lc(gs8) lp(dot))) 
}

graph combine yr2006 yr2007 yr2008 yr2009, rows(2) altshrink scheme(s1color) //create Fig---# unique vendors across markets


*** One figure
graph drop _all


cdfplot nvals,by(year) opt1(lc(green blue cyan red) lp(shortdash dash_dot longdash solid) xtitle("# leading vendors") aspectratio(0.6) legend(rows(1) label(1 "2006") label(2 "2007") label(3 "2008") label(4 "2009") size(vsmall) symxsize(*0.3) margin(zero)) scheme(s1color) yline(0.2 0.4 0.6 0.8 1,lc(gs8) lp(dot)) xline(1 2 3 4 5 6 7 8 9 10,lc(gs8) lp(dot)) xlabel(0 1 2 3 4 5 6 7 8 9 10))  









******************* Create data for ### Fig A4 ### and Fig A4 is created using GIS, with the help from a colleague
*******************
*******************
clear
use "./prepare/allbasic.dta",clear

gen adopt=(prechs>1)
gen const=1
bys hsa year prechs:egen numvnd=total(const)
bys hsa year:egen totadp=total(adopt)
gen vndshare=numvnd/totadp
replace numvnd=0 if totadp==0
duplicates drop hsa year prechs,force
drop if prechs==1
export excel hsa year prechs vndshare using "./prepare/inputs4figA4.xlsx", firstrow(var) replace













